package com.ds.client.controller;


import com.badlogic.gdx.graphics.g2d.tiled.TiledMap;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.ds.client.actors.Car;


public class collisionController {
	public static int tile[][];
	public static TiledMap map;
	public static final int nOffset = 8;
	public static final int mOffset = 8;

	static {
		tile = map.layers.get(0).tiles;
	}

	public static boolean collision(Car car, Car other, Stage stage){
		
		return carCollision(car, other) || raceCollison(car);
	}
	public static boolean carCollision(Car car, Car other) {
		return Math.pow(car.getX() - other.getX(), 2)
				+ Math.pow(car.getY() - other.getY(), 2) <= 1600;
	}


	public static boolean raceCollison(Car car) {

		int m1 = MathUtils.ceilPositive((car.getX() - mOffset) / map.tileWidth); //
		int n1 = tile.length
				- MathUtils.ceilPositive((car.getY() - nOffset) / map.tileHeight);

		int m2 = MathUtils.ceilPositive((car.getX() + car.width / 2 - mOffset )
				/ map.tileWidth); //
		int n2 = tile.length
				- MathUtils.ceilPositive((car.getY() - mOffset) / map.tileHeight);

		int m3 = MathUtils.ceilPositive((car.getX() + car.width - nOffset)
				/ map.tileWidth); //
		int n3 = tile.length
				- MathUtils.ceilPositive((car.getY() - nOffset ) / map.tileHeight);

		// hight
		int m4 = MathUtils.ceilPositive((car.getX() + car.width - mOffset)
				/ map.tileWidth); //
		int n4 = tile.length
				- MathUtils.ceilPositive((car.getY() + car.height / 2 - nOffset)
						/ map.tileHeight);

		int m5 = MathUtils.ceilPositive((car.getX() + car.width - mOffset)
				/ map.tileWidth); //
		int n5 = tile.length
				- MathUtils.ceilPositive((car.getY() + car.height - nOffset)
						/ map.tileHeight);

		int m6 = MathUtils.ceilPositive((car.getX() + car.width / 2 - mOffset)
				/ map.tileWidth); //
		int n6 = tile.length
				- MathUtils.ceilPositive((car.getY() + car.height - nOffset)
						/ map.tileHeight);

		int m7 = MathUtils.ceilPositive((car.getX() - mOffset) / map.tileWidth );
		int n7 = tile.length
				- MathUtils.ceilPositive((car.getY() + car.height - nOffset)
						/ map.tileHeight);

		int m8 = MathUtils.ceilPositive((car.getX() - mOffset) / map.tileWidth);
		int n8 = tile.length
				- MathUtils.ceilPositive((car.getY() + car.height/2 - nOffset)
						/ map.tileHeight);
		
		if (tile[n1][m1] != 0 || tile[n7][m7] != 0 || tile[n2][m2] != 0
				|| tile[n3][m3] != 0 || tile[n4][m4] != 0 || tile[n5][m5] != 0
				|| tile[n6][m6] != 0 || tile[n8][m8] != 0) {
			return true;
		} else
			return false;
	}


}
